package com.ruoyi.web.service;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.web.domain.Disease;
import com.ruoyi.web.domain.DiseaseNew;
import com.ruoyi.web.domain.KeyAndValue;
import com.ruoyi.web.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 *  html调用 thymeleaf 实现字典读取
 * 
 * @author lfy
 */
@Service("commonService")
public class CommonService
{

    @Resource
    private DiseaseMapper diseaseMapper;
    @Autowired
    private RedisCache redisCache;
    @Resource
    private CountryMapper countryMapper;
    @Resource
    private ClinicalMapper clinicalMapper;
    @Resource
    private HospitalMapper hospitalMapper;
    @Resource
    private DrugMapper drugMapper;


    //根据父ID查询缓存的新疾病
    public List<DiseaseNew> selectDiseaseByPid(Long pid){
        List<DiseaseNew> cacheList = redisCache.getCacheList("dis:disease:disease_pid_" + pid);
        return cacheList;
    }

    /*
    * 肿瘤专区疾病列表 pid=3 && id != 8
    * */
    public List<Map<String,Object>> selPidForTumor(){
        /*List<Disease> cacheList = redisCache.getCacheList("yibanlv:disease:disease_pid_" + 3);
        for(Iterator<Disease> car = cacheList.iterator(); car.hasNext();){
            Disease disease = car.next();
            if (disease.getId() == 8L){
                car.remove();
                break;
            }
        }*/
        Disease disease = new Disease();
        disease.setPid(3L);
        List<Disease> list= diseaseMapper.selectDiseaseList(disease);
        List<Map<String,Object>> a = dataBeanToMap(list);
//        for(Map<String,Object> map:a){
//           Object i = map.get("id");
//           String id = String.valueOf(i);
//            map.put("id",id);
//        }
        for(Iterator<Map<String,Object>> car = a.iterator(); car.hasNext();){
            Map<String, Object> next = car.next();
            Object i = next.get("id");
            if (Integer.parseInt(String.valueOf(i)) == 8 ){
                car.remove();
                continue;
            }
            String id = String.valueOf(i);
            next.put("id",id);
        }
        return a;
    }

    public List<Map<String,Object>> dataBeanToMap(List<Disease> menuInfoList ){
        //将list实体类转换成list map
        List<Map<String,Object>>mapList=new ArrayList<>();
        for(Disease disease:menuInfoList){
            Map<String,Object>map=new HashMap<>();
            map=JSONObject.parseObject(JSONObject.toJSONString(disease),Map.class);
            mapList.add(map);
        }
        return mapList;
    }



    public boolean contains(List<String> ids,String id){
        boolean r=false;
       if(ids.indexOf(id)>-1){
           r=true;
       }
        return r;
    }


    public List<KeyAndValue> disaseKeyValue() {
        return diseaseMapper.diseaseKeyValue(new Disease());
    }

    /**
     * 查询国家的keyName value
     */
    public List<KeyAndValue> countryKeyAndValue() {
        return  countryMapper.countryKeyAndValue();
    }

    /*
    *  查新临床招募的keyvalue
    * */
    public List<KeyAndValue> getClinicalKeyAndValue(){return clinicalMapper.getClinicalKeyAndValue();}

    /*
    * 查询医院的keyvalue
    * */
    public List<KeyAndValue> getHospitalKeyAndValue(){return hospitalMapper.getHospitalKeyAndValue();}

    /**
     * 查询药品的keyName value
     */
    public List<KeyAndValue> drugKeyAndValue() {
        return  drugMapper.drugKeyAndValue();
    }
}
